package com.example.meterialproject.exts

import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.annotation.FloatRange
import androidx.annotation.IntRange
import androidx.core.view.ViewCompat
import com.example.meterialproject.exts.szj.szj

/**
 *
 * @ClassName: ViewGroup
 * @Author: android 超级兵
 * @CreateDate: 4/28/22$ 9:56 AM$
 * TODO
 */

/*
 * 作者:android 超级兵
 * 创建时间: 4/28/22 10:09 AM
 * TODO 默认给viewGroup添加view(最好parentView是linearLayout)
 */
fun ViewGroup.addRandomColorView(
    @IntRange(from = 0) maxChild: Int = 20,// 添加个数
    @FloatRange(from = 0.0, to = 1.0) alpha: Float = 0.4f,//默认透明度
    isCallBack: Boolean = true, // 是否监听点击回调
    callBack: (View, Int, Int) -> Unit = { _, _, _ -> },// 点击回调
) {
    (0..maxChild).map { position ->
        val view = context.szj.randomColor((255 * alpha).toInt()).let { color ->
            View(context).apply {

                setBackgroundColor(color)

                if (isCallBack) {
                    click { callBack(this, color, position) }
                }
                // 设置 transitionName
                ViewCompat.setTransitionName(this, "$position")
            }
        }

        addView(view, ViewGroup.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            50.dp(context).toInt()
        ))
    }

}